<template>
  <div class="myForm">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form :label-width="labelWidth">
      <el-row>
        <template v-for="item in formItems" :key="item.label">
          <el-col v-bind="colLayout">
            <el-form-item
              v-if="!item.isHidden"
              :label="item.label"
              :rules="item.rules"
              :style="itemStyle"
            >
              <template
                v-if="item.type === 'input' || item.type === 'password'"
              >
                <el-input
                  :placeholder="item.placeholder"
                  v-bind="item.otherOptions"
                  :show-password="item.type === 'password'"
                  v-model="formData[`${item.field}`]"
                />
              </template>
              <!-- 其他选择器 -->
              <template
                v-else-if="item.type === 'select' && item.field !== 'roles'"
              >
                <el-select
                  :placeholder="item.placeholder"
                  v-bind="item.otherOptions"
                  style="width: 100%"
                  v-model="formData[`${item.field}`]"
                >
                  <el-option
                    v-for="option in item.options"
                    :key="option.value"
                    :label="option.title"
                    :value="option.value"
                    >{{ option.title }}</el-option
                  >
                </el-select>
              </template>
              <!-- 角色选择器 -->
              <template
                v-else-if="item.type === 'select' && item.field === 'roles'"
              >
                <el-select
                  :placeholder="item.placeholder"
                  v-bind="item.otherOptions"
                  style="width: 100%"
                  value-key="id"
                  v-model="formData[`${item.field}`]"
                >
                  <el-option
                    v-for="option in item.options"
                    :key="option.id"
                    :label="option.cname"
                    :value="option"
                    >{{ option.cname }}</el-option
                  >
                </el-select>
              </template>
              <template v-else-if="item.type === 'datepicker'">
                <el-date-picker
                  v-bind="item.otherOptions"
                  style="width: 100%"
                  v-model="formData[`${item.field}`]"
                ></el-date-picker>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, type PropType, ref, watch } from "vue"
import type { FormItem } from "../types"

export default defineComponent({
  props: {
    // 父组件双向绑定传过来的值
    modelValue: {
      type: Object,
      required: true
    },
    formItems: {
      type: Array as PropType<FormItem[]>,
      default: () => []
    },
    labelWidth: {
      type: String,
      default: "80px"
    },
    itemStyle: {
      type: Object,
      default: () => ({ padding: "10px 40px" })
    },
    colLayout: {
      type: Object,
      default: () => ({
        xl: 6, // ≥1920px   4个
        lg: 8, // ≥1200px   3个
        md: 12, //≥992px    2个
        sm: 24, // ≥768px   1个
        xs: 24 // <768px    1个
      })
    }
  },
  emits: ["update:modelValue"],
  setup(props, { emit }) {
    // 父组件双向绑定传过来的值
    const formData = ref({ ...props.modelValue })

    // 通知父组件修改formData
    watch(formData, newValue => emit("update:modelValue", newValue), {
      deep: true
    })
    return {
      formData
    }
  }
})
</script>

<style scoped lang="less">
.myForm {
  padding-top: 22px;
}
</style>
